﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public partial class TQPPLAN : ViewBase
    {
        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TQPPLAN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VQPPLAN)MV).QPLAN = value;
                LOADQP();
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPPLAN;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKEY===========
        public static readonly DependencyProperty QPKEYProperty = DependencyProperty.Register("QPKEY",
            typeof(string), typeof(TQPPLAN),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKEYChanged));
        public string QPKEY
        {
            get
            {
                return (string)GetValue(QPKEYProperty);
            }
            set
            {
                SetValue(QPKEYProperty, value);
             }
        }
        private static void OnQPKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPPLAN;
            if (src != null)
            {
                src.QPKEY = (string)e.NewValue;
            }
        }
        #endregion
        #region ==dwQP===========
        public static readonly DependencyProperty dwQPProperty = DependencyProperty.Register("dwQP",
            typeof(DataRow), typeof(TQPPLAN),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQPChanged));
        public DataRow dwQP
        {
            get
            {
                return (DataRow)GetValue(dwQPProperty);
            }
            set
            {
                SetValue(dwQPProperty, value);
            }
        }
        private static void OdwQPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPPLAN;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQP = row;
            }
        }
        #endregion
        #region ==FTAUTOQC===========
        public static readonly DependencyProperty FTAUTOQCProperty = DependencyProperty.Register("FTAUTOQC",
            typeof(bool), typeof(TQPPLAN),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTAUTOQCChanged));
        public bool FTAUTOQC
        {
            get
            {
                return (bool)GetValue(FTAUTOQCProperty);
            }
            set
            {
                SetValue(FTAUTOQCProperty, value);
                ((VQPPLAN)MV).FTAUTOQC = value;
            }
        }
        private static void OnFTAUTOQCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQPPLAN;
            if (src != null)
            {
                src.FTAUTOQC = (bool)e.NewValue;
            }
        }
        #endregion

        public TQPPLAN()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VQPPLAN();
            MESSAGES += ";";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwQP = dwMaster;
            if (dwQP == null)
            {
                QPKEY = "";
            }
            else
            {
                QPKEY = dwQP["QPPPNRKRS"].ToString() + dwQP["QPPPLFDNR"].ToString();  
            }
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE; 
        }

        public void LOADQP()
        {
            if (QPLAN.Trim().Length == 0)
            {
                TBMain.Clear();
                return;
            } 
            DataRow dwData = TBMain.NewRow();
            dwData["QPPMANDANT"] = app.Mandant;
            dwData["QPPPNRKRS"] = QPLAN.Substring(0,2);
            dwData["QPPPLFDNR"] = QPLAN.Substring(2, 6); 
            DataSet SResult = ((VQPPLAN)MV).QueryQPPLAN(dwData, 1);
            MergeData(SResult, true);
        } 
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwQP == null)
                return;
            ((VQPPLAN)MV).SAVEQP(dwQP);
            cQP04.BatchSave();
            base.SaveCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            ((VQPPLAN)MV).NEWQP();
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender,e);
        } 
    }
}
